module textbox(
input CLOCK_50, 
input leftKey,
input rightKey,
input rstKey,
input iCLK1,iCLK2,
input [9:0] px,
input [9:0] py,
output reg valid,
output pixel,

output LEDG0,
output LEDG1,
output LEDG2,

inout PS2_DAT,
inout PS2_CLK,
input [7:0] received_data
);


assign LEDG0 = S[0];
assign LEDG1 = S[1];
assign LEDG2 = S[2];

parameter COLS = 64;
parameter ROWS = 16;

parameter TIMER = 50000000;

parameter START=3'd0;
parameter GAME =3'd1;
parameter LOSE =3'd2;
parameter WIN  =3'd3;
parameter EASY =3'd4;

reg lkey;
reg rkey;

reg[2:0]S;
reg[2:0]NS;
reg[2:0]lives;
reg [59:0]bricks;
reg [26:0] count;
reg [5:0] count2;
reg [9:0] paddle;
reg [9:0] ballX;
reg [9:0] ballY;
reg [9:0] ballXv;
reg [9:0] ballYv;
	

reg ballXd; // 1=left 0=right
reg ballYd; // 0=up 1=down

//assign pixel = 1'b1;

//State transitions
always @(*)

begin
	case (S)
		START:
			begin
				if(rightKey == 1 && leftKey == 0 || received_data[7:4]==4'h6 && received_data[3:0]==4'hB) NS=EASY;
				else if(rightKey == 0 && leftKey == 1) NS=GAME;
				else NS=START;
			end

		GAME://all transitions work
			begin
				if(bricks==60'b111111111111111111111111111111111111111111111111111111111111)NS=WIN;
				else if(lives<1)NS=LOSE;
				else NS=GAME;
			end
		EASY:
			begin
				if(bricks==60'b111111111111111111111111111111111111111111111111111111111111)NS=WIN;
				else NS=EASY;
			end
		WIN:
		begin
				if((rightKey == 1 && leftKey == 0)||rightKey == 0 && leftKey ==1) NS=START;
				else NS=WIN;
		end
		LOSE:
		begin
				if((rightKey == 1 && leftKey == 0)||rightKey == 0 && leftKey ==1) NS=START;
				else NS=LOSE;
		end
	endcase
end

//state update
always @(posedge CLOCK_50 or negedge rstKey)
begin

if (rstKey == 1'b0)
	begin
		S <= START;
	end
else
	begin
		S <= NS;
	end
end


//clock block

always @(posedge CLOCK_50 or negedge rstKey)
begin
if( rstKey == 1'b0)
begin
count <= 0;
paddle <= 258;
ballX <= 333;
ballY <= 200;
ballXv <= 3;
ballYv <= 3;
ballYd <= 1;
ballXd <= 1;
bricks <= 60'd0;
lives <=4;
end
else
 
//cases
case(S)

START://set everything to start position 
begin
paddle <= 258;
ballX <= 333;
ballY <= 200;
count2 <= count2 +1;
// psudo random velocity
if( count2 % 6 == 0)
begin
	ballXd <= 0;
	ballXv <= 3;
	ballYv <= 3;
end
else if( count2 % 6 == 1)
begin
	ballXd <= 0;
	ballXv <= 2;
	ballYv <= 4;
end
else if( count2 % 6 == 2)
begin
	ballXd <= 0;
	ballXv <= 1;
	ballYv <= 5;
end
else if( count2 % 6 == 3)
begin
	ballXd <= 1;
	ballXv <= 3;
	ballYv <= 3;
end
else if( count2 % 6 == 4)
begin
	ballXd <= 1;
	ballXv <= 2;
	ballYv <= 4;
end
else 
begin
	ballXd <= 1;
	ballXv <= 1;
	ballYv <= 5;
end



ballYd <= 1;
bricks <= 60'd0;
lives <=4;
end //end of START


GAME:
begin
//lives<=3'd4;
if (count >= TIMER)
begin
count <= 0;
count2 = count2 + 1;
//paddle movement
if( rightKey == 1 && leftKey == 0 && paddle <520)
paddle <= paddle + 6; 
if(rightKey == 0 && leftKey ==1 && paddle >3)
paddle <= paddle - 6;


//ball movement
if (ballYd == 1)
ballY <= ballY + ballYv;
else ballY <= ballY - ballYv;

if (ballXd == 1)
ballX <= ballX + ballXv;
else ballX <= ballX - ballXv;

//direction change
if (ballY <= 9)
ballYd <= 1;
if (ballY >= 460 && ballX > paddle && ballX < paddle+120)
begin	
ballYd <= 0;  //velocity change
	if(ballX < paddle + 30)
		begin 
			if( ballXd == 0 && ballYv > 1)
			begin
				ballYv <= ballYv - 1;
				ballXv <= ballXv + 1;
			end
			if( ballXd == 1 && ballXv > 1)
			begin
				ballYv <= ballYv + 1;
				ballXv <= ballXv - 1;
			end
		end
	else if(ballX > paddle - 30 + 120)
		begin
			if(ballXv > 1 && ballXd == 0)
			begin
				ballYv <= ballYv + 1;
				ballXv <= ballXv - 1;
			end
			if(ballYv > 1 && ballXd == 1)
			begin
				ballYv <= ballYv - 1;
				ballXv <= ballXv + 1;
			end
		end
end
//  && ballX > paddle && ballX < paddle
if (ballX <= 5 || ballX >= 700)
ballXd <= 1;
else if ( ballX >= 625)
ballXd <= 0;

if(ballY>=480)
begin
lives <= lives-1;

//random again
if( count2 % 6 == 0)
begin
	ballXd <= 0;
	ballXv <= 3;
	ballYv <= 3;
end
else if( count2 % 6 == 1)
begin
	ballXd <= 0;
	ballXv <= 2;
	ballYv <= 4;
end
else if( count2 % 6 == 2)
begin
	ballXd <= 0;
	ballXv <= 1;
	ballYv <= 5;
end
else if( count2 % 6 == 3)
begin
	ballXd <= 1;
	ballXv <= 3;
	ballYv <= 3;
end
else if( count2 % 6 == 4)
begin
	ballXd <= 1;
	ballXv <= 2;
	ballYv <= 4;
end
else 
begin
	ballXd <= 1;
	ballXv <= 1;
	ballYv <= 5;
end
ballX<=333;
ballY<=200;
end

//ball collisions with bricks
//column1 works checked
if((ballX>=0 && ballX<=62))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[0]<=1'b0)
	begin
	bricks[0]<=1'b1;
		if(ballX+10>=72) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[1]<=1'b0)
	begin
	bricks[1]<=1'b1;
		if((ballX+10>=72) )ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[2]<=1'b0) 
	begin
	bricks[2]<=1'b1;
		if(ballX+10>=72) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[3]<=1'b0) 
		begin
	bricks[3]<=1'b1;
		if(ballX+10>=72) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[4]<=1'b0)
		begin
	bricks[4]<=1'b1;
		if(ballX+10>=72) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[5]<=1'b0)
	begin
	bricks[5]<=1'b1;
		if(ballX+10>=72) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column2 works checked
else if((ballX>=64 && ballX<=126)||(ballX+10>=64&&ballX+10<=126))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[6]<=1'b0)
	begin
	bricks[6]<=1'b1;
		if((ballX+10>=136)||(ballX<=54)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[7]<=1'b0)
	begin
	bricks[7]<=1'b1;
		if((ballX+10>=136)||(ballX<=54)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[8]<=1'b0) 
	begin
	bricks[8]<=1'b1;
		if((ballX+10>=136)||(ballX<=54)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[9]<=1'b0) 
		begin
	bricks[9]<=1'b1;
		if((ballX+10>=136)||(ballX<=54)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[10]<=1'b0)
		begin
	bricks[10]<=1'b1;
		if((ballX+10>=136)||(ballX<=54)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[11]<=1'b0)
	begin
	bricks[11]<=1'b1;
		if((ballX+10>=136)||(ballX<=54)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end
//column3 works
else if((ballX>=128 && ballX<=192)||(ballX+10>=128 && ballX+10<=192))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[12]<=1'b0)
	begin
	bricks[12]<=1'b1;
		if((ballX+10>=202)||(ballX<=118)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[13]<=1'b0)
	begin
	bricks[13]<=1'b1;
		if((ballX+10>=202)||(ballX<=118)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[14]<=1'b0) 
	begin
	bricks[14]<=1'b1;
		if((ballX+10>=202)||(ballX<=118)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[15]<=1'b0) 
		begin
	bricks[15]<=1'b1;
		if((ballX+10>=202)||(ballX<=118)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[16]<=1'b0)
		begin
	bricks[16]<=1'b1;
		if((ballX+10>=202)||(ballX<=118)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[17]<=1'b0)
	begin
	bricks[17]<=1'b1;
		if((ballX+10>=202)||(ballX<=118)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column4 works 
else if((ballX>=194 && ballX<=256)||(ballX+10>=194 && ballX+10<=256))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[18]<=1'b0)
	begin
//changed to 256 from 256	
	bricks[18]<=1'b1;
		if((ballX+10>=266)||(ballX<=184)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[19]<=1'b0)
	begin
	bricks[19]<=1'b1;
		if((ballX+10>=266)||(ballX<=184)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[20]<=1'b0) 
	begin
	bricks[20]<=1'b1;
		if((ballX+10>=266)||(ballX<=184)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[21]<=1'b0) 
		begin
	bricks[21]<=1'b1;
		if((ballX+10>=266)||(ballX<=184)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[22]<=1'b0)
		begin
	bricks[22]<=1'b1;
		if((ballX+10>=266)||(ballX<=184)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[23]<=1'b0)
	begin
	bricks[23]<=1'b1;
		if((ballX+10>=266)||(ballX<=184)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column5 coded
else if((ballX>=258 && ballX<=320)||(ballX+10>=258 && ballX+10<=320))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[24]<=1'b0)
	begin
//changed to 330 from 320	
	bricks[24]<=1'b1;
		if((ballX+10>=330)||(ballX<=248)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[25]<=1'b0)
	begin
	bricks[25]<=1'b1;
		if((ballX+10>=330)||(ballX<=248)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[26]<=1'b0) 
	begin
	bricks[26]<=1'b1;
		if((ballX+10>=330)||(ballX<=248)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[27]<=1'b0) 
		begin
	bricks[27]<=1'b1;
		if((ballX+10>=330)||(ballX<=248)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[28]<=1'b0)
		begin
	bricks[28]<=1'b1;
		if((ballX+10>=330)||(ballX<=248)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[29]<=1'b0)
	begin
	bricks[29]<=1'b1;
		if((ballX+10>=330)||(ballX<=248)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column6 coded
else if((ballX>=322 && ballX<=384)||(ballX+10>=322 && ballX+10<=384))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[30]<=1'b0)
	begin
	bricks[30]<=1'b1;
		if((ballX+10>=384)||(ballX<=312)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[31]<=1'b0)
	begin
	bricks[31]<=1'b1;
		if((ballX+10>=384)||(ballX<=312)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[32]<=1'b0) 
	begin
	bricks[32]<=1'b1;
		if((ballX+10>=384)||(ballX<=312)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[33]<=1'b0) 
		begin
	bricks[33]<=1'b1;
		if((ballX+10>=384)||(ballX<=312)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[34]<=1'b0)
		begin
	bricks[34]<=1'b1;
		if((ballX+10>=384)||(ballX<=312)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[35]<=1'b0)
	begin
	bricks[35]<=1'b1;
		if((ballX+10>=384)||(ballX<=312)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column7 coded
else if((ballX>=386 && ballX<=448)||(ballX+10>=386 && ballX+10<=448))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[36]<=1'b0)
	begin
	bricks[36]<=1'b1;
		if((ballX+10>=448)||(ballX<=376)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[37]<=1'b0)
	begin
	bricks[37]<=1'b1;
		if((ballX+10>=448)||(ballX<=376)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[38]<=1'b0) 
	begin
	bricks[38]<=1'b1;
		if((ballX+10>=448)||(ballX<=376)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[39]<=1'b0) 
		begin
	bricks[39]<=1'b1;
		if((ballX+10>=448)||(ballX<=376)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[40]<=1'b0)
		begin
	bricks[40]<=1'b1;
		if((ballX+10>=448)||(ballX<=376)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[41]<=1'b0)
	begin
	bricks[41]<=1'b1;
		if((ballX+10>=448)||(ballX<=376)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column8 coded
else if((ballX>=450 && ballX<=512)||(ballX+10>=450 && ballX+10<=512))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[42]<=1'b0)
	begin
	bricks[42]<=1'b1;
		if((ballX+10>=512)||(ballX<=440)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[43]<=1'b0)
	begin
	bricks[43]<=1'b1;
		if((ballX+10>=512)||(ballX<=440)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[44]<=1'b0) 
	begin
	bricks[44]<=1'b1;
		if((ballX+10>=512)||(ballX<=440)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[45]<=1'b0) 
		begin
	bricks[45]<=1'b1;
		if((ballX+10>=512)||(ballX<=440)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[46]<=1'b0)
		begin
	bricks[46]<=1'b1;
		if((ballX+10>=512)||(ballX<=440)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[47]<=1'b0)
	begin
	bricks[47]<=1'b1;
		if((ballX+10>=512)||(ballX<=440)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column9
else if((ballX>=514 && ballX<=576)||(ballX+10>=514 && ballX+10<=576))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[48]<=1'b0)
	begin
	bricks[48]<=1'b1;
		if((ballX+10>=576)||(ballX<=504)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[49]<=1'b0)
	begin
	bricks[49]<=1'b1;
		if((ballX+10>=576)||(ballX<=504)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[50]<=1'b0) 
	begin
	bricks[50]<=1'b1;
		if((ballX+10>=576)||(ballX<=504)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[51]<=1'b0) 
		begin
	bricks[51]<=1'b1;
		if((ballX+10>=576)||(ballX<=504)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[52]<=1'b0)
		begin
	bricks[52]<=1'b1;
		if((ballX+10>=576)||(ballX<=504)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[53]<=1'b0)
	begin
	bricks[53]<=1'b1;
		if((ballX+10>=576)||(ballX<=504)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end

//column10
else if((ballX>=578 && ballX<=640)||(ballX+10>=578 && ballX+10<=640))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[54]<=1'b0)
	begin
	bricks[54]<=1'b1;
		if((ballX+10>=650)||(ballX<=568)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[55]<=1'b0)
	begin
	bricks[55]<=1'b1;
		if((ballX+10>=650)||(ballX<=568)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[56]<=1'b0) 
	begin
	bricks[56]<=1'b1;
		if((ballX+10>=650)||(ballX<=568)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[57]<=1'b0) 
		begin
	bricks[57]<=1'b1;
		if((ballX+10>=650)||(ballX<=568)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[58]<=1'b0)
		begin
	bricks[58]<=1'b1;
		if((ballX+10>=650)||(ballX<=568)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[59]<=1'b0)
	begin
	bricks[59]<=1'b1;
		if((ballX+10>=650)||(ballX<=568)) ballXd<=(ballXd==1'b1)?0:1;
		else ballYd<=(ballYd==1'b1)?0:1;
	end
end


end

else
begin
count <= count + 20;
count2 <= count2 + 1;
end
end

LOSE:
begin
lives<=3'd2;
	if (count >= TIMER)
	begin
		count <= 0;
	end

	else
	begin
		count <= count + 20;
	end
end 






WIN:
begin
lives<=3'd3;
	if (count >= TIMER)
	begin
		count <= 0;
	end

	else
	begin
		count <= count + 20;
	end
end 





EASY:
begin
lives<=0;
if (count >= TIMER)
begin
count <= 0;

//paddle movement
if( rightKey == 1 && leftKey == 0 && paddle <520)
paddle <= paddle + 6; 
if( rightKey == 0 && leftKey ==1 && paddle >3)
paddle <= paddle - 6;


//ball movement
if (ballYd == 1)
ballY <= ballY + ballYv;
else ballY <= ballY - ballYv;

if (ballXd == 1)
ballX <= ballX + ballXv;
else ballX <= ballX - ballXv;

//direction change
if (ballY <= 6)
ballYd <= 1;
if (ballY >= 460 && ballX > paddle && ballX < paddle+120)	
ballYd <= 0;
//  && ballX > paddle && ballX < paddle
if (ballX <= 3 )
ballXd <= 1;
else if ( ballX >= 627)
ballXd <= 0;

if(ballY>=480)
begin
//lives <= lives-1;
ballX<=333;
ballY<=200;
end

//ball collisions with bricks
//column1 works
if((ballX>=0 && ballX<=62))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[0]<=1'b0)
	begin
	bricks[0]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[1]<=1'b0)
	begin
	bricks[1]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[2]<=1'b0) 
	begin
	bricks[2]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[3]<=1'b0) 
		begin
	bricks[3]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[4]<=1'b0)
		begin
	bricks[4]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[5]<=1'b0)
	begin
	bricks[5]<=1'b1;
	end
end

//column2 works
else if((ballX>=64 && ballX<=126)||(ballX+10>=64&&ballX+10<=126))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[6]<=1'b0)
	begin
	bricks[6]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[7]<=1'b0)
	begin
	bricks[7]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[8]<=1'b0) 
	begin
	bricks[8]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[9]<=1'b0) 
		begin
	bricks[9]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[10]<=1'b0)
		begin
	bricks[10]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[11]<=1'b0)
	begin
	bricks[11]<=1'b1;
	end
end
//column3 works
else if((ballX>=128 && ballX<=192)||(ballX+10>=128 && ballX+10<=192))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[12]<=1'b0)
	begin
	bricks[12]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[13]<=1'b0)
	begin
	bricks[13]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[14]<=1'b0) 
	begin
	bricks[14]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[15]<=1'b0) 
		begin
	bricks[15]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[16]<=1'b0)
		begin
	bricks[16]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[17]<=1'b0)
	begin
	bricks[17]<=1'b1;
	end
end

//column4 works 
else if((ballX>=194 && ballX<=256)||(ballX+10>=194 && ballX+10<=256))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[18]<=1'b0)
	begin
	bricks[18]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[19]<=1'b0)
	begin
	bricks[19]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[20]<=1'b0) 
	begin
	bricks[20]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[21]<=1'b0) 
		begin
	bricks[21]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[22]<=1'b0)
		begin
	bricks[22]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[23]<=1'b0)
	begin
	bricks[23]<=1'b1;
	end
end

//column5 coded
else if((ballX>=258 && ballX<=320)||(ballX+10>=258 && ballX+10<=320))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[24]<=1'b0)
	begin
	bricks[24]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[25]<=1'b0)
	begin
	bricks[25]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[26]<=1'b0) 
	begin
	bricks[26]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[27]<=1'b0) 
		begin
	bricks[27]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[28]<=1'b0)
		begin
	bricks[28]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[29]<=1'b0)
	begin
	bricks[29]<=1'b1;
	end
end

//column6 coded
else if((ballX>=322 && ballX<=384)||(ballX+10>=322 && ballX+10<=384))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[30]<=1'b0)
	begin
	bricks[30]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[31]<=1'b0)
	begin
	bricks[31]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[32]<=1'b0) 
	begin
	bricks[32]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[33]<=1'b0) 
		begin
	bricks[33]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[34]<=1'b0)
		begin
	bricks[34]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[35]<=1'b0)
	begin
	bricks[35]<=1'b1;
	end
end

//column7 coded
else if((ballX>=386 && ballX<=448)||(ballX+10>=386 && ballX+10<=448))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[36]<=1'b0)
	begin
	bricks[36]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[37]<=1'b0)
	begin
	bricks[37]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[38]<=1'b0) 
	begin
	bricks[38]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[39]<=1'b0) 
		begin
	bricks[39]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[40]<=1'b0)
		begin
	bricks[40]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[41]<=1'b0)
	begin
	bricks[41]<=1'b1;
	end
end

//column8 coded
else if((ballX>=450 && ballX<=512)||(ballX+10>=450 && ballX+10<=512))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[42]<=1'b0)
	begin
	bricks[42]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[43]<=1'b0)
	begin
	bricks[43]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[44]<=1'b0) 
	begin
	bricks[44]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[45]<=1'b0) 
		begin
	bricks[45]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[46]<=1'b0)
		begin
	bricks[46]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[47]<=1'b0)
	begin
	bricks[47]<=1'b1;
	end
end

//column9
else if((ballX>=514 && ballX<=576)||(ballX+10>=514 && ballX+10<=576))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[48]<=1'b0)
	begin
	bricks[48]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[49]<=1'b0)
	begin
	bricks[49]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[50]<=1'b0) 
	begin
	bricks[50]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[51]<=1'b0) 
		begin
	bricks[51]<=1'b1;

	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[52]<=1'b0)
		begin
	bricks[52]<=1'b1;

	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[53]<=1'b0)
	begin
	bricks[53]<=1'b1;

	end
end

//column10
else if((ballX>=578 && ballX<=640)||(ballX+10>=578 && ballX+10<=640))
begin
if(((ballY>=44 && ballY<=64)||(ballY+10>=44 && ballY+10<=64)) && bricks[54]<=1'b0)
	begin
	bricks[54]<=1'b1;
	end
else if(((ballY>=66 && ballY<=86)||(ballY+10>=66 && ballY+10<=86)) && bricks[55]<=1'b0)
	begin
	bricks[55]<=1'b1;
	end
else if(((ballY>=88 && ballY<=108) ||(ballY+10>=88 && ballY+10<=108)) && bricks[56]<=1'b0) 
	begin
	bricks[56]<=1'b1;
	end
else if(((ballY>=110 && ballY<=130) ||(ballY+10>=110 && ballY+10<=130)) && bricks[57]<=1'b0) 
		begin
	bricks[57]<=1'b1;
	end
else if(((ballY>=132 && ballY<=152) ||(ballY+10>=132 && ballY+10<=152)) && bricks[58]<=1'b0)
		begin
	bricks[58]<=1'b1;
	end
else if(((ballY>=154 && ballY<=174) ||(ballY+10>=154 && ballY+10<=174)) && bricks[59]<=1'b0)
	begin
	bricks[59]<=1'b1;
	end
end


end

else
begin
count <= count + 20;
end
end



endcase
end

//John loomis' textbox code


// instantiate text memory
		chars mem1(
			.address(character_address),
			.clock(~iCLK1),
			.wren(1'b0),
			.q(qchr)
			);

		// calculate character address
		wire [9:0] character_address = {py[7:4],px[8:3]};
		wire [7:0] qchr;

		// instantiate sysfont memory
		sysfont mem2(
			.address(address),
			.clock(iCLK2),
			.q(q)
			);
		wire [10:0] address = {	qchr[6:0], py[3:0] };
		wire [7:0] q;
		reg rom_mux_output;



//ened




always	//drawing

case(S)

	START:
	begin
	
		// test that we are inside the textbox
		
			if (px>=COLS*8) valid = 1'b0;
			else if (py>=ROWS*16) valid = 1'b0;
			else if (py<=120 && py>=70)valid=1'b0;
			else valid = 1'b1;
			
		// Mux to pick off correct rom data bit from 8-bit word
			// for on screen character generation
			case (px[2:0])
			0: rom_mux_output = q[0];
			1: rom_mux_output = q[7];
			2: rom_mux_output = q[6];
			3: rom_mux_output = q[5];
			4: rom_mux_output = q[4];
			5: rom_mux_output = q[3];
			6: rom_mux_output = q[2];
			7: rom_mux_output = q[1];
			endcase
			
			//assign pixel = rom_mux_output;
	end
	WIN:
	begin	

			// test that we are inside the textbox
		
			if (px>=COLS*8) valid = 1'b0;
			else if (py>=ROWS*16) valid = 1'b0;
			else if (py<=120 && py>=95|| py<=70 || py>= 200)valid=1'b0;
			else valid = 1'b1;
			
		// Mux to pick off correct rom data bit from 8-bit word
			// for on screen character generation
			case (px[2:0])
			0: rom_mux_output = q[0];
			1: rom_mux_output = q[7];
			2: rom_mux_output = q[6];
			3: rom_mux_output = q[5];
			4: rom_mux_output = q[4];
			5: rom_mux_output = q[3];
			6: rom_mux_output = q[2];
			7: rom_mux_output = q[1];
			endcase
	end
		
	LOSE:
	begin
	// test that we are inside the textbox
		
			if (px>=COLS*8) valid = 1'b0;
			else if (py>=ROWS*16) valid = 1'b0;
			else if (py<=95 && py>=80|| py<=70 || py>= 200)valid=1'b0;
			else valid = 1'b1;
			
		// Mux to pick off correct rom data bit from 8-bit word
			// for on screen character generation
			case (px[2:0])
			0: rom_mux_output = q[0];
			1: rom_mux_output = q[7];
			2: rom_mux_output = q[6];
			3: rom_mux_output = q[5];
			4: rom_mux_output = q[4];
			5: rom_mux_output = q[3];
			6: rom_mux_output = q[2];
			7: rom_mux_output = q[1];
			endcase
	end




default:
begin

rom_mux_output = 1'b1;


 //paddle and ball
if	(px>= paddle && px<=paddle + 120 && py >= 470) valid = 1'b1;
else if(px >= ballX && px <= ballX +10 && py>= ballY && py<= ballY +10) valid = 1'b1;
//lives
else if(py>=10 && py<=20)
	begin
	if(lives==3'd4 && ((px>=10 && px<=20)||(px>=30 && px<=40)||(px>=50 && px<=60))) valid=1'b1;
	else if(lives==3'd3 && ((px>=10 && px<=20)||(px>=30 && px<=40))) valid=1'b1;
	else if(lives==3'd2 && (px>=10 && px<=20)) valid=1'b1;
	else valid=1'b0;
	end
//bricks
//column1

else if((px>=0 && px<=62))
begin
if(py>=44 && py<=64 && bricks[0]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[1]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[2]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[3]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[4]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[5]<=1'b0) valid=1'b1;
else valid=1'b0;
end
//column2
else if((px>=64 && px<=126))
begin
if(py>=44 && py<=64 && bricks[6]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[7]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[8]<=1'b0)valid=1'b1;
else if(py>=110 && py<=130 && bricks[9]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[10]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[11]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column3
 else if((px>=128 && px<=192))
begin
if(py>=44 && py<=64 && bricks[12]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[13]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[14]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[15]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[16]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[17]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column4
 else if((px>=194 && px<=256))
begin
if(py>=44 && py<=64 && bricks[18]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[19]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[20]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[21]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[22]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[23]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column5
else if((px>=258 && px<=320))
begin
if(py>=44 && py<=64 && bricks[24]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[25]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[26]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[27]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[28]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[29]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column6
else if((px>=322 && px<=384))
begin
if(py>=44 && py<=64 && bricks[30]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[31]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[32]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[33]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[34]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[35]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column7
else if((px>=386 && px<=448))
begin
if(py>=44 && py<=64 && bricks[36]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[37]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[38]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[39]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[40]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[41]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column8
else if((px>=450 && px<=512))
begin
if(py>=44 && py<=64 && bricks[42]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[43]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[44]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[45]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[46]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[47]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column9
else if((px>=514 && px<=576))
begin
if(py>=44 && py<=64 && bricks[48]<=1'b0) valid=1'b1;
else if(py>=66 && py<=86 && bricks[49]<=1'b0) valid=1'b1;
else if(py>=88 && py<=108 && bricks[50]<=1'b0) valid=1'b1;
else if(py>=110 && py<=130 && bricks[51]<=1'b0) valid=1'b1;
else if(py>=132 && py<=152 && bricks[52]<=1'b0) valid=1'b1;
else if(py>=154 && py<=174 && bricks[53]<=1'b0) valid=1'b1;
else valid=1'b0;
end

//column10
else if((px>=578 && px<=640))
begin
if(py>=44 && py<=64 && bricks[54]<=1'b0) valid=1'b1;
	else if(py>=66 && py<=86 && bricks[55]<=1'b0) valid=1'b1;
	else if(py>=88 && py<=108 && bricks[56]<=1'b0) valid=1'b1;
	else if(py>=110 && py<=130 && bricks[57]<=1'b0) valid=1'b1;
	else if(py>=132 && py<=152 && bricks[58]<=1'b0) valid=1'b1;
	else if(py>=154 && py<=174 && bricks[59]<=1'b0) valid=1'b1;
else valid=1'b0;
end

else valid = 1'b0;

end //end default

endcase

assign pixel = rom_mux_output; //1'b1;

endmodule